#!/usr/bin/env python3
"""
EchokitBot micro-ROS Voice Control System Launch File

Architecture: [EchoKit Server] → [Voice Control Node] → [ROS2 Topics] → [micro-ros-agent] → [EchokitBot UDP]

This launch file starts:
1. Voice Control Node (connects to EchoKit Server and publishes to /cmd_vel)
2. Smart Voice Processor (enhanced voice command processing)
3. System Monitor (monitors system health)
"""

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node

def generate_launch_description():
    # Declare launch arguments
    server_url_arg = DeclareLaunchArgument(
        'server_url',
        default_value='ws://192.168.2.28:8060/ws/robot',
        description='EchoKit server WebSocket URL'
    )
    
    log_level_arg = DeclareLaunchArgument(
        'log_level',
        default_value='info',
        description='Log level (debug, info, warn, error)'
    )
    
    # Enhanced LLM Bridge Voice Control Node (connects to EchoKit Server and publishes to /cmd_vel)
    voice_control_node = Node(
        package='echokitbot_microros_voice',
        executable='voice_control_node',
        name='voice_control_node',
        parameters=[{
            'server.url': LaunchConfiguration('server_url'),
            'server.reconnect_interval': 5.0,
            'server.connection_timeout': 10.0,
            'movement.max_linear_velocity': 0.5,
            'movement.max_angular_velocity': 1.0,
            'movement.default_linear_velocity': 0.3,
            'movement.default_angular_velocity': 0.5,
            'safety.command_timeout': 30.0,
            'safety.emergency_stop_timeout': 10.0,
            'audio.confirmation_enabled': True,
            'audio.language': 'auto',
            'commands.confidence_threshold': 0.7,
            'debug.enable_verbose_logging': False
        }],
        output='screen',
        arguments=['--ros-args', '--log-level', LaunchConfiguration('log_level')]
    )
    
    # Smart Voice Processor (enhanced voice command processing)
    smart_voice_processor = Node(
        package='echokitbot_microros_voice',
        executable='smart_voice_processor',
        name='smart_voice_processor',
        output='screen',
        arguments=['--ros-args', '--log-level', LaunchConfiguration('log_level')]
    )
    
    # System Monitor (monitors system health)
    system_monitor = Node(
        package='echokitbot_microros_voice',
        executable='system_monitor',
        name='system_monitor',
        parameters=[{
            'echokitbot_ip': '192.168.2.42',
            'echokit_server': '192.168.2.28:8060'
        }],
        output='screen',
        arguments=['--ros-args', '--log-level', LaunchConfiguration('log_level')]
    )
    
    return LaunchDescription([
        server_url_arg,
        log_level_arg,
        
        LogInfo(msg='Starting EchokitBot micro-ROS Voice Control System...'),
        LogInfo(msg=['EchoKit Server: ', LaunchConfiguration('server_url')]),
        LogInfo(msg='Architecture: EchoKit → Voice Control → ROS2 Topics → micro-ros-agent → EchokitBot'),
        LogInfo(msg='Expected micro-ros-agent on localhost:8888 (UDP)'),
        LogInfo(msg='Expected EchokitBot at 192.168.2.42 (via micro-ROS)'),
        
        voice_control_node,
        smart_voice_processor,
        system_monitor,
    ])